package com.example.filter;

import com.example.settings.domain.User;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class LoginFilter implements Filter {
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse reponse = (HttpServletResponse) servletResponse;

        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        String servletpath = request.getServletPath();
        if ("/login.jsp".equals(servletpath) || "/settings/user/login.do".equals(servletpath)){
            filterChain.doFilter(servletRequest,servletResponse);
        }else {

            if (user !=null){
                filterChain.doFilter(servletRequest,servletResponse);
            }else {
                reponse.sendRedirect(request.getContextPath()+"/login.jsp");
            }


        }


    }
}
